package com.epalmpay.handler;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.epalmpay.service.weixin.IWxBaseService;

import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutTextMessage;

/**
 * 
 * @author Binary Wang
 *
 */
@Component
public class UnsubscribeHandler extends AbstractHandler {
	
	
	@Resource
	private IWxBaseService wxBaseService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
            Map<String, Object> context, WxMpService wxMpService,
            WxSessionManager sessionManager) {
       try {
    	   String openId = wxMessage.getFromUser();
           this.logger.info("取消关注用户 OPENID: " + openId);
           wxBaseService.updateUnsubscribe(openId,wxMessage.getToUser());
           // TODO 可以更新本地数据库为取消关注状态
           WxMpXmlOutTextMessage m= WxMpXmlOutMessage.TEXT()
           .content(wxMessage.getToUser() + "，用户取消关注")
           .fromUser(wxMessage.getToUser())
           .toUser(wxMessage.getFromUser())
           .build();
   		    logger.info("UnsubscribeHandler" + m.getContent());
   		    return m;
	} catch (Exception e) { 
		e.printStackTrace();
	}
	return null; 
    }

}
